package com.supostacks.infrastructure.repository;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.supostacks.domain.repository.IUserRepository;
import com.supostacks.domain.vo.UserVo;
import com.supostacks.infrastructure.context.DefaultContext;
import com.supostacks.infrastructure.context.EntityUtil;
import com.supostacks.infrastructure.dao.UserDao;
import com.supostacks.infrastructure.po.User;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class UserRepository extends DefaultContext implements IUserRepository {

    @Resource
    private UserDao userDao;

    @Override
    public String insert(UserVo userVo) {
        try {
            User user = new User();
            EntityUtil.copyFields(userVo, user);

            int insert = userDao.insert(user);
            if (insert > 0) {
                return user.getId();
            }
            return null;

        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteById(String id) {
        userDao.deleteById(id);
    }

    @Override
    public String updateById(UserVo userVo) {
        try {
            User user = new User();
            EntityUtil.copyFields(userVo, user);
            int i = userDao.updateById(user);
            if (i > 0) {
                return user.getId();
            }
            return "";
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public UserVo selectById(String id) {
        User user = userDao.selectById(id);
        if (user != null) {
            return poToVo(user);
        }
        return null;
    }

    @Override
    public List<UserVo> selectList(JSONObject param) {
        QueryWrapper<User> queryWrapper = where(param);
        List<User> users = userDao.selectList(queryWrapper);
        return buildList(users);
    }

    @Override
    public JSONObject selectByPage(JSONObject options, int pageNo, int pageSize) {
        QueryWrapper<User> queryWrapper = where(options);

        Page<User> infos = new Page<>(pageNo, pageSize);
        IPage<User> iPage = userDao.selectPage(infos, queryWrapper);

        List<User> records = iPage.getRecords();

        JSONObject result = new JSONObject();
        result.put("total", iPage.getTotal());
        result.put("list", records);

        return result;
    }

    public List<UserVo> buildList(List<User> users) {
        return users.stream().map(this::poToVo).collect(Collectors.toList());
    }

    public UserVo poToVo(User user) {
        try {
            UserVo userVo = new UserVo();
            EntityUtil.copyFields(user, userVo);
            return userVo;
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
